/// 厦门大学计算机专业 | 华为开发专家(HDE)
/// 专注《零基础学编程系列》  http://lblbc.cn/blog
/// 包含：鸿蒙 | Java | 安卓 | 前端 | Flutter | iOS | 小程序
/// 公众号：蓝不蓝编程
import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      debugShowCheckedModeBanner: false,
      home: const MyHomePage(title: '选号工具'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String num1 = '0';
  String num2 = '0';
  String num3 = '0';
  String num4 = '0';
  String num5 = '0';
  String num6 = '0';
  String num7 = '0';

  void createNums() {
    final numbers = List<int>.generate(33, (index) => index + 1)..shuffle();
    num1 = numbers[0].toString();
    num2 = numbers[1].toString();
    num3 = numbers[2].toString();
    num4 = numbers[3].toString();
    num5 = numbers[4].toString();
    num6 = numbers[5].toString();
    num7 = random(16).toString();
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    createNums();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                buildNumView(num1),
                buildNumView(num2),
                buildNumView(num3),
                buildNumView(num4),
                buildNumView(num5),
                buildNumView(num6),
                buildNumView(num7),
              ],
            ),
            const SizedBox(height: 20),
            buildButton(),
          ],
        ),
      ),
    );
  }

  Widget buildNumView(String number) {
    return Container(
      width: 40.0,
      height: 40.0,
      margin: const EdgeInsets.all(5),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Colors.grey, width: 1),
      ),
      child: Center(
        child: Text(
          number,
          style: const TextStyle(
            fontSize: 16,
            color: Colors.black,
          ),
        ),
      ),
    );
  }

  Widget buildButton() {
    return ElevatedButton(
      onPressed: createNums,
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
        shape: const StadiumBorder(),
        minimumSize: const Size(100, 50),
      ),
      child: const Text('生成'),
    );
  }

  String random(int max) {
    return (Random().nextInt(max) + 1).toString();
  }
}
